<?php
namespace app\admin\controller;

use think\Controller;
use think\Log;
use think\Exception;

class Activity extends Base
{

    /**
     * 获取用户列表
     * @author wcy
     * @version 2017年5月5日15:27:11
     */
    public function activityList(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $data = $this->deDataTablesJson();
                $draw=$data['draw'];
                $start=$data["start"];
                $length=$data["length"];
                $search="";
                if(!empty($_POST["search"])){
                    $search=$_POST["search"];
                }
                $data=model("Activity")->activityList($search,$start,$length);
                if(!$data){
                    return json(["draw"=>$draw,"recordsTotal"=>0,"recordsFiltered"=>0,"data"=>[]]);
                }else{
                    return json(["draw"=>$draw,"recordsTotal"=>$data["count"],"recordsFiltered"=>$data["count"],"data"=>$data["data"]]);
                }
            }else{
                return view("activitylist");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
    /**
     * 添加活动
     * @author wcy
     * @version 2017年5月5日15:27:11
     */
    public function addActivity(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $data["activity_name"]=$_POST["activity_name"];
                $id="";
                $count=model("Activity")->getActivityName($id,$data["activity_name"]);
                if($count>0){
                    return json(["code"=>-1,"msg"=>"活动名称已经存在"]);
                }
                $data["activity_href"]=$_POST["activity_href"];
                $data["status"]=$_POST["status"];
                $data["activity_img"]=$this->upload("activity/upload");
                $res=model("Activity")->addActivity($data);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"添加成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"添加失败"]);
                }
            }else{
                return view("addactivity");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
    /**
     * 编辑活动
     * @author wcy
     * @version 2017年5月5日16:12:55
     */
    public function editActivity(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $id=$_POST["id"];
                $data["activity_name"]=$_POST["activity_name"];
                $count=model("Activity")->getActivityName($id,$data["activity_name"]);
                if($count>0){
                    return json(["code"=>-1,"msg"=>"活动名称已经存在"]);
                }
                $data["activity_href"]=$_POST["activity_href"];
                $data["status"]=$_POST["status"];
                $activity_img=$this->upload("activity/upload");
                if(!empty($activity_img)){
                    $data["activity_img"]=$activity_img;
                }
                $res=model("Activity")->editActivity($id,$data);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"修改成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"修改失败"]);
                }
            }else{
                $id=$_GET["id"];
                $data=model("activity")->getActivityById($id);
                $this->assign("activity_data",$data);
                return view("editactivity");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("editactivity");
        }
    }
    /**
     * 修改状态
     * @author wcy
     * @version 2017年5月5日16:34:05
     */
    public function editStatus(){
        try{
            $id=$_POST["id"];
            $data["status"]=$_POST["status"];
            $res=model("Activity")->editActivity($id,$data);
            if($res!==false){
                return json(["code"=>1,"msg"=>"修改成功"]);
            }else{
                return json(["code"=>-1,"msg"=>"修改失败"]);
            }
        }catch (Exception $e){
            return json(["code"=>-1,"msg"=>"系统出错，请刷新页面"]);
        }
    }
    /**
     * 删除活动
     * @author wcy
     * @version 2017年5月5日16:05:56
     */
    public function delActivity(){
        try{
            $id=$_POST["id"];
            $res=model("Activity")->delActivity($id);
            if($res!==false){
                return json(["code"=>1,"msg"=>"删除成功"]);
            }else{
                return json(["code"=>-1,"msg"=>"删除失败"]);
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return json(["code"=>-1,"msg"=>"系统出错，请刷新页面"]);
        }
    }

}